package com.example.springboot2.Control;

import com.alibaba.fastjson.JSONObject;
import com.example.springboot2.CommonHelper.RedisHelper;
import com.example.springboot2.DB_Helper.DataBase;
import com.example.springboot2.DB_Helper.JDBCUtils;
import com.example.springboot2.Model.Sys_Module;
import com.example.springboot2.Model.Sys_Role;
import com.example.springboot2.Model.Sys_User;
import com.example.springboot2.Service.Sys_RoleService;
import com.example.springboot2.Service.Sys_UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/Sys_Role")
public class Sys_RoleController extends BaseController {
    private Sys_RoleService sys_roleService=new Sys_RoleService();
    private DataBase<Sys_Role> database=new DataBase<Sys_Role>();

    @Resource
    private RedisHelper redisHelper=new RedisHelper();
    @PostMapping("/GetPageList")
    public ReturnListTable GetPageList(@RequestParam int limit, @RequestParam int page)
    {
        ReturnListTable result=new ReturnListTable();
        result.code=0;
        result.data=sys_roleService.GetPageList(limit,page);
        result.count=sys_roleService.GetCount();
        return result;
    }
    @PostMapping("/SaveRole")
    public  ReturnString SaveRole(@RequestParam()String KeyValue,@RequestParam()String StrEntity) throws IllegalAccessException {
        ReturnString result=new ReturnString();
        result.Code=200;
        Sys_Role module=  JSONObject.parseObject(StrEntity,Sys_Role.class);
        String sql="";
        if(KeyValue.isEmpty())
        {
            module.Sys_RoleId= UUID.randomUUID().toString();
            sql=database.add(module);
        }
        else
        {
            sql=database.update(module);
        }
        Connection conn = JDBCUtils.findConnection();
        PreparedStatement pre = null;
        ResultSet res = null;
        try {
            pre = conn.prepareStatement(sql);
            pre.execute();
            result.Message="保存成功";
            return  result;

        } catch (SQLException e) {
            result.Code=201;
            result.Message=e.toString();
            //e.printStackTrace();
        }
        return result;
    }
    @PostMapping("/GetListByUser")
    public ReturnListObject GetListByUser(@RequestParam()String UserId)
    {
        ReturnListObject result=new ReturnListObject();
        result.Code=200;

//        String token=request.getHeader("token");
//        Sys_User user=(Sys_User) redisHelper.get(token);
        List<Sys_Role> RoleList=sys_roleService.GetUserRoleList(UserId);

        result.Result=RoleList;
        return result;
    }
    @PostMapping("/SaveRoleRelation")
    public ReturnString SaveRoleRelation(@RequestParam()String roleId,@RequestParam()List<String> authIds)
    {
        ReturnString result=new ReturnString();
        result.Code=200;
        String sql=" delete from Sys_RoleModule where Sys_RoleId='"+roleId+"' ";
        for (String audthid:
             authIds) {
            sql=sql+" Insert into  Sys_RoleModule values(NEWID(),'"+roleId+"','"+audthid+"') ";
        }
        Connection conn = JDBCUtils.findConnection();
        PreparedStatement pre = null;
        ResultSet res = null;
        try {
            pre = conn.prepareStatement(sql);
            pre.execute();
            result.Message="保存成功";
            return  result;

        } catch (SQLException e) {
            result.Code=201;
            result.Message=e.toString();
            //e.printStackTrace();
        }
        return result;
    }
}
